package cn.gzsxt.music.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzsxt.music.mapper.AdminMapper;
import cn.gzsxt.music.mapper.ModularMapper;
import cn.gzsxt.music.mapper.PowerMapper;
import cn.gzsxt.music.query.AdminQueryObject;
import cn.gzsxt.music.service.AdminService;
import cn.gzsxt.music.util.ConstantUtil;

@Service
@Transactional // 配置事物管理器
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper mapper;

	@Autowired
	private ModularMapper modularMapper;
	
	@Autowired
	private PowerMapper powerMapper;
	
	@Autowired
	private HttpSession session;

	@Override
	public int insert(Map<String, Object> params) {
		return mapper.insert(params);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> login(Map<String, Object> params) {
		return mapper.selectNameAndPass(params);
	}

	@Override
	public Map<String, Object> selectAdminDataByCondition(AdminQueryObject qo) {
		// 组装前台easyui datagrid对应需要数据格式的map集合
		Map<String, Object> dataGridMap = new HashMap<>();
		dataGridMap.put("total", mapper.selectAdminTotalByCondition(qo));
		dataGridMap.put("rows", mapper.selectAdminListByCondition(qo));

		return dataGridMap;
	}

	@Override
	public int updateAdmin(Map<String, Object> params) {
		return mapper.updateAdmin(params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMenu() {
		
		//获取当前HttpSession中登录的用户角色(获取当前用户拥有的权限集合)
		Map<String, Object> role = (Map<String, Object>) session.getAttribute(ConstantUtil.ROLE_IN_SESSION);
		Map<String, Object> admin = (Map<String, Object>) session.getAttribute(ConstantUtil.USER_IN_SESSION);
		//获取所有的权限
		String role_action = (String) role.get("role_action");
		//admin/adminPage.do,admin/deleteAdminById.do,admin/updateAdmin.do,admin/adminList.do
		
		//查询所有的模块
		List<Map<String, Object>> allModulars = modularMapper.selectAllModulars();

		Iterator<Map<String, Object>> it = allModulars.iterator();
		while(it.hasNext()) {
			//每个模块
			Map<String, Object> modular = it.next();
			//根据模块id查询所有的权限
			List<Map<String, Object>> powers = powerMapper.selectPowersByModularId((Long)modular.get("id"));
			
			//循环每个权限,用于组装 attributes:{url:'xxx/xxx.do'}结构数据
			Iterator<Map<String, Object>> powersIt = powers.iterator();
			while(powersIt.hasNext()) {
				//具体某个权限
				Map<String, Object> power = powersIt.next();
				
				//判断当前登录用户有的权限集合 是否包含 power 对象中的 权限power_action
				
				//用户的权限集合 : admin/adminPage.do,admin/deleteAdminById.do,admin/updateAdmin.do,admin/adminList.do
				//每次循环的 菜单的 对应的 power_action  :  admin/adminPage.do  role/rolePage.do 
				String power_action =  (String)power.get("power_action");
				//如果不包含,说明当前用户的权限没有此功能,把集合中对应的 权限对象删除
				if(!"admin".equals(admin.get("admin_name"))) {
					if(!role_action.contains(power_action)) {
						powersIt.remove();
					}
				}	
				
				//创建 urlMap集合
				Map<String,Object> urlMap = new HashMap<>();
				urlMap.put("url", power.get("power_action"));
				
				//设值power的 attribute属性
				power.put("attributes", urlMap);
				
				//组装完毕以后删除power_action属性
				power.remove("power_action");
				
			}
			
			//将权限设值为 模块的子节点
			modular.put("children", powers);
			
			if(!"admin".equals(admin.get("admin_name"))) {
				//如果模块下面没有对于的权限功能,模块也没有必要显示
				if(powers.size() == 0) {
					//把模块从集合中删除
					it.remove();
				}
			}
			
			
		}
		
		return allModulars;
	}

	@Override
	public List<Map<String, Object>> getMusicMenu() {
		
		Map<String, Object> role = (Map<String, Object>) session.getAttribute(ConstantUtil.ROLE_IN_SESSION);
		Map<String, Object> admin = (Map<String, Object>) session.getAttribute(ConstantUtil.USER_IN_SESSION);
		String role_action = (String) role.get("role_action");
		List<Map<String, Object>> allModulars = modularMapper.selectAllModularsById((long)4);
		
		Iterator<Map<String, Object>> it = allModulars.iterator();
		while(it.hasNext()) {
			Map<String, Object> modular = it.next();
			List<Map<String, Object>> powers = powerMapper.selectPowersByModularId((Long)modular.get("id"));
			Iterator<Map<String, Object>> powersIt = powers.iterator();
			while(powersIt.hasNext()) {
				Map<String, Object> power = powersIt.next();
				String power_action =  (String)power.get("power_action");
				if(!"admin".equals(admin.get("admin_name"))) {
					if(!role_action.contains(power_action)) {
						powersIt.remove();
					}
				}	
				
				Map<String,Object> urlMap = new HashMap<>();
				urlMap.put("url", power.get("power_action"));
				power.put("attributes", urlMap);
				power.remove("power_action");
			}
			
			modular.put("children", powers);
			if(!"admin".equals(admin.get("admin_name"))) {
				if(powers.size() == 0) {
					it.remove();
				}
			}
		}
		return allModulars;
	}

}
